<?php

/**
 * @Author: Wang chunsheng  email:2192138785@qq.com
 * @Date:   2023-04-27 10:59:48
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2023-06-25 11:14:52
 */

namespace Common\Services;

use Common\BaseService;
use Common\Helpers\DebugHelper;
use Common\Models\sys\SysOperationLog;
use Common\Services\Sys\DeviceService;


class OperationLog extends BaseService
{
    public static function addLog($member_id, $device_type, $project_id,$behavior_type, $server_name, $device_mac, $type, $instruct, $start_time, $remark, $callback): int
    {
        $SysOperationLog = new SysOperationLog();

        $device_id = DeviceService::initDevice($device_mac, $server_name, $device_type);

        $data =  [
            'member_id' => $member_id,
            'behavior_type' => $behavior_type,
            'project_id' => $project_id,
            'device_id' => $device_id,
            'device_type' => $device_type,
            'device_mac' => $device_mac,
            'type' => $type,
            'instruct' => $instruct,
            'start_time' => $start_time,
            'remark' => $remark,
        ];
        $logId = $SysOperationLog->addInfo($data);
        if(is_callable($callback)){
            DebugHelper::consoleWrite('写日志数据回调',[
                'logId' => $logId
            ]);
            $callback($device_mac, $logId, $instruct);
        }
//        ReceptacleJob::checkOp($device_mac, $logId, $instruct);
//        self::upLogId($server_name, $logId, $device_mac);
        return  $logId;
    }


//    public static function upLogId($server_name, $logId, $device_mac)
//    {
//        switch ($server_name) {
//            case 'Receptacle': //开关类设备
//                $DeviceReceptacle = new DeviceReceptacle();
//                $DeviceReceptacle->updataInfo([
//                    'log_id' => $logId
//                ], [
//                    'mac' => $device_mac
//                ]);
//                break;
//
//            default:
//                # code...
//                break;
//        }
//    }

    public static function editLogJobRes($id, $job_res): bool
    {
        $SysOperationLog = new SysOperationLog();

        $data = [
            'job_res' => (int) $job_res
        ];

        return  $SysOperationLog->updateInfo($data, [
            'id' => $id
        ]);
    }


    public static function editLogJobFdStatus($id, $fd_status, $job_time): bool
    {
        $SysOperationLog = new SysOperationLog();

        $data = [
            'fd_status' => (int) $fd_status,
            'job_time' => $job_time
        ];

        return  $SysOperationLog->updateInfo($data, [
            'id' => $id
        ]);
    }

    public static function editLogSendTime($id): bool
    {
        $SysOperationLog = new SysOperationLog();

        $data = [
            'send_time' => date('Y-m-d H:i:s', time())
        ];

        return  $SysOperationLog->updateInfo($data, [
            'id' => $id
        ]);
    }

    public static function editLogResultCode($id, $result_code): bool
    {
        $SysOperationLog = new SysOperationLog();

        $data = [
            'result_time' => date('Y-m-d H:i:s', time()),
            'result_code' => $result_code
        ];

        return  $SysOperationLog->updateInfo($data, [
            'id' => $id
        ]);
    }

    public static function editLogResultTime($id): bool
    {
        $SysOperationLog = new SysOperationLog();

        $data = [
            'result_time' => date('Y-m-d H:i:s', time())
        ];

        return  $SysOperationLog->updateInfo($data, [
            'id' => $id
        ]);
    }

    public static function detailLogCode(int $logId): int
    {
        $SysOperationLog = new SysOperationLog();
        $result_code =  $SysOperationLog->query()->where(['id' => $logId])->value('result_code');
        return (int) $result_code;
    }

    public static function detailLogById(int $logId): array
    {
        $SysOperationLog = new SysOperationLog();
        return $SysOperationLog->query()->where(['id' => $logId])->first()->toArray();
    }



    public static function getLog(int $page, int $pageSize, int $project_id = 0, int $device_id = 0): array
    {
        $SysOperationLog = new SysOperationLog();
        $where = [];

        if ($project_id) {
            $where['project_id'] = $project_id;
        }

        if ($device_id) {
            $where['device_id'] = $device_id;
        }

        return $SysOperationLog->getAll($page, $pageSize, $where, [
            'create_time' => 'DESC'
        ]);
    }
}
